package myTest.web;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.Before;
import org.junit.Test;

public class HttpClientTest {
	private HttpClient client;

	@Before
	public void init() {
		client = new HttpClient();
	}

	@Test
	public void test1() {
		//即表示java client将按照浏览器的方式来自动处理Cookie
		client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
		client.getParams().setParameter("http.protocol.single-cookie-header", true);

		//hotmail登录之前需要设置当前时间的Cookie： 
		client.getState().addCookie(new Cookie("login.live.com", "CkTst", "G" + new Date().getTime()));

	}

	protected void setHeaders(HttpMethod method, String encoding) {
		method.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;");
		method.setRequestHeader("Accept-Language", "zh-cn");
		method.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
		method.setRequestHeader("Accept-Charset", encoding);
		method.setRequestHeader("Keep-Alive", "300");
		method.setRequestHeader("Connection", "Keep-Alive");
		method.setRequestHeader("Cache-Control", "no-cache");
		method.setRequestHeader("Referer", "");
		method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	}

	@Test
	public void testSina() throws HttpException, IOException {
		//来自
		//http://s.yanghao.org/program/viewdetail.php?i=226239
		HttpClient client = new HttpClient();

		client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);

		PostMethod post = new PostMethod("http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.16)");

		String data = System.currentTimeMillis() + "";

		String nonce = "xxssse";
		//
		NameValuePair[] nvps = new NameValuePair[] { //
		new NameValuePair("entry", "weibo"), //
		new NameValuePair("gateway", "1"), //
		new NameValuePair("from", ""), //
		new NameValuePair("savestate", "7"), //
		new NameValuePair("useticket", "1"), //
		new NameValuePair("ssosimplelogin", "1"), //
		new NameValuePair("vsnf", "1"), //
		new NameValuePair("vsnval", ""), //
		new NameValuePair("su", "encodeAccount(微博用户名)"), //
		new NameValuePair("service", "miniblog"), //
		new NameValuePair("servertime", data), //
		new NameValuePair("nonce", nonce), //
		new NameValuePair("pwencode", "wsse"), //
		new NameValuePair("sp", "微博密码 data, nonce"), //
		new NameValuePair("encoding", "UTF-8"), //
		new NameValuePair("returntype", "META"), //
		new NameValuePair( //
		"url", //
		"http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack") };

		post.setRequestBody(nvps);

		client.executeMethod(post);

		// 此处是测试用的。获取到【正在登录页面跳转的url，此url为实际登录获取cookie的url】
		String url = post.getResponseBodyAsString().substring(post.getResponseBodyAsString().indexOf("http://weibo.com/ajaxlogin.php?"), post.getResponseBodyAsString().indexOf("code=0") + 6);

		// 获取到实际url进行连接
		GetMethod getMethod = new GetMethod(url);

		client.executeMethod(getMethod);

		GetMethod weiBoMethod = new GetMethod("http://需要登录才能访问的微博地址");

		// 实际访问微博
		int status = client.executeMethod(getMethod);
	}
}

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//